// pages/index/tmp/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    title: '个人宝贝', //物品名
    tmpCode: '', //物品编码
    code: '', //二维码码值
    showCode: '', //二维码显示值
    visitNum: 0,
    useNum: 0,
    lasttime: '', //最近时间
    imgList: [], //图片集合
    attrList: [], //属性集合
    commentTotal: 0,
    commentList: [], //评论集合
    followTotal: 0,
    followList: [], //关注集合
    // 以下是弹窗
    pop1: 1, //弹窗物品名：0出现1关闭
    pop2: 1, //弹窗属性
    pop3: 1, //弹窗图集
    attrKey: '', attrVal: '', attrid: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    this.getOne(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 获取数据
  getOne: function (v) {
    var that = this;
    wx.request({
      method: 'POST',
      url: app.globalData.domainXcx + '/tmp/one',
      data: {id: v},
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res);
        if (res.code!=0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        that.setData({
          id: res.data.id,
          title: res.data.title, 
          tmpCode: res.data.tmpCode, 
          code: res.data.code, 
          showCode: res.data.showCode,
          visitNum: res.data.visitNum,
          useNum: res.data.useNum,
          lasttime: res.data.lasttime, 
          imgList: res.data.imgList,
          attrList: res.data.attrList,
          commentTotal: res.data.commentTotal,
          commentList: res.data.commentList,
          followTotal: res.data.followTotal,
          followList: res.data.followList,
        });
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // 返回
  toBack: function (e) {
    console.log(e);
    wx.navigateBack({
      delta: 1
    }); return
  },

  // 选择图片
  chooseImage: function (e) {
    wx.showLoading({
        title: '正在处理图片...',
    });
    wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: res => {
            // var base64 = 'data:image/png;base64,' + wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64")
            // 获取文件路径
            const filePath = res.tempFilePaths[0];
            console.log(filePath);
            // 读取文件
            wx.getFileSystemManager().readFile({
                filePath: filePath,
                encoding: 'base64',
                success: res => {
                    // 将文件内容转换为base64字符串
                    const base64Data = 'data:image/png;base64,' + res.data;
                    // 可以在这里使用base64字符串
                    console.log(base64Data);
                    // wx.showToast({
                    //   title: base64Data, icon: 'none'
                    // }); return
                    this.upload(base64Data);
                },
                fail: err => {
                    console.error('读取文件失败', err);
                    wx.showToast({
                      title: '读取文件失败', icon: 'none'
                    }); return
                }
            });
        },
        fail: err => {
            console.error('选择图片失败', err);
            wx.showToast({
              title: '选择图片失败', icon: 'none'
            }); return
        }
    });
  },

  // 上传图片
  upload: function (e) {
    console.log(e);
    var that = this;
    wx.request({
        method: 'POST',
        url: app.globalData.domainXcx + '/tmp/upload',
        data: {id: this.data.id, base64: e},
        timeout: 10000, //接口超时10S
        success: function (res) {
            res = res.data;
            console.log(res);
            if (res.code!=0) {
                wx.showToast({
                    title: res.msg, icon: 'none'
                }); return
            }
            wx.showToast({
              title: '上传成功', icon: 'none'
            });
            that.viewReload();
        },
        fail: function () {
            console.log('error');
        }
    });
  },

  // 图片删除
  imgDel: function (e) {
    console.log(e);
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.request({
        method: 'POST',
        url: app.globalData.domainXcx + '/tmp/img/del',
        data: {id: id},
        timeout: 10000, //接口超时10S
        success: function (res) {
            res = res.data;
            console.log(res);
            if (res.code!=0) {
                wx.showToast({
                    title: res.msg, icon: 'none'
                }); return
            }
            wx.showToast({
              title: '删除成功', icon: 'none'
            });
            that.viewReload();
        },
        fail: function () {
            console.log('error');
        }
    });
  },

  // 弹窗
  popShow: function (e) {
    console.log(e);
    var type = e.currentTarget.dataset.type;
    var attrid = e.currentTarget.dataset.id;
    var key = e.currentTarget.dataset.key;
    var val = e.currentTarget.dataset.val;
    if (type == 1) {
        this.setData({pop1: 0});
    } else if (type == 2) {
        this.setData({
            pop2: 0,
            attrid: attrid,
            attrKey: key,
            attrVal: val,
        });
    } else if (type == 3) {
        this.setData({pop3: 0});
    } else {
        wx.showToast({
          title: '弹窗错误', icon: 'none'
        }); return
    }
  },

  // 关闭弹窗
  popClose: function (e) {
    console.log(e);
    var type = e.currentTarget.dataset.type;
    var attrid = e.currentTarget.dataset.attrid;
    var key = e.currentTarget.dataset.key;
    var val = e.currentTarget.dataset.val;
    if (type == 1) {
        this.setData({pop1: 1});
    } else if (type == 2) {
        this.setData({
            pop2: 1,
            attrid: 0,
            attrKey: '',
            attrVal: '',
        });
    } else if (type == 3) {
        this.setData({pop3: 1});
    } else {
        wx.showToast({
          title: '弹窗错误', icon: 'none'
        }); return
    }
  },

  // 记录title
  toTitle: function (e) {
    // console.log(e);
    this.setData({
        title: e.detail.value,
    });
  },

  // 更新物品名
  saveNick: function (e) {
    var that = this;
    var formData = {
        id: this.data.id,
        title: this.data.title,
    };
    wx.request({
        method: 'POST',
        url: app.globalData.domainXcx + '/tmp/modify/title',
        data: formData,
        timeout: 10000, //接口超时10S
        success: function (res) {
            res = res.data;
            console.log(res);
            if (res.code!=0) {
                wx.showToast({
                    title: res.msg, icon: 'none'
                }); return
            }
            wx.showToast({
              title: '修改成功', icon: 'none'
            });
            that.viewReload();
        },
        fail: function () {
            console.log('error');
        }
    });
  },

  // 记录attrKey
  toAttrKey: function (e) {
    this.setData({
        attrKey: e.detail.value,
    });
  },

  // 记录attrVal
  toAttrVal: function (e) {
    this.setData({
        attrVal: e.detail.value,
    });
  },

  // 更新属性
  attrUpdate: function (e) {
    var that = this;
    console.log(e);
    var formData = {
        id: this.data.id,
        attrid: this.data.attrid,
        key: this.data.attrKey,
        val: this.data.attrVal,
    };
    console.log(formData); //return
    wx.request({
        method: 'POST',
        url: app.globalData.domainXcx + '/tmp/attr/modify',
        data: formData,
        timeout: 10000, //接口超时10S
        success: function (res) {
            res = res.data;
            console.log(res);
            if (res.code!=0) {
                wx.showToast({
                    title: res.msg, icon: 'none'
                }); return
            }
            wx.showToast({
              title: '更新成功', icon: 'none'
            });
            that.viewReload();
        },
        fail: function () {
            console.log('error');
        }
    });
  },

  // 属性删除
  attrDel: function (e) {
    var that = this;
    // console.log(e); return
    var attrid = e.currentTarget.dataset.id;
    var formData = {
        attrid: attrid,
    };
    console.log(formData); //return
    wx.request({
        method: 'POST',
        url: app.globalData.domainXcx + '/tmp/attr/del',
        data: formData,
        timeout: 10000, //接口超时10S
        success: function (res) {
            res = res.data;
            console.log(res);
            if (res.code!=0) {
                wx.showToast({
                    title: res.msg, icon: 'none'
                }); return
            }
            wx.showToast({
              title: '删除成功', icon: 'none'
            });
            that.viewReload();
        },
        fail: function () {
            console.log('error');
        }
    });
  },

  // 刷新页面
  viewReload: function (e) {
    var param = '?id=' + this.data.id;
    wx.navigateTo({
        url: '/pages/index/tmp/index' + param,
    }); 
  },
})